﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Converters;
using System.Xml.Linq;
using Puzzler.Common;
using Puzzler.Plugin.Interface;
using Puzzler.Plugin.Interface.Attributes;
using Puzzler.Plugin.Interface.Exceptions;

namespace Plugin.Default.SwedenGrid
{
    public enum SelectionGoDirection
    {
        None,
        Left,
        Right,
        Top,
        Bottom
    }

    public sealed class SwedenGridColorModel : DependencyObject, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public static readonly DependencyProperty RegularTextProperty =
            DependencyProperty.Register("RegularText", typeof (Brush), typeof (SwedenGridColorModel),
                                        new PropertyMetadata(Brushes.Black));

        public Brush RegularText
        {
            get { return (Brush) GetValue(RegularTextProperty); }
            set { SetValue(RegularTextProperty, value); }
        }

        public static readonly DependencyProperty RegularBackgroundProperty =
            DependencyProperty.Register("RegularBackground", typeof (Brush), typeof (SwedenGridColorModel),
                                        new PropertyMetadata(Brushes.White));

        public Brush RegularBackground
        {
            get { return (Brush) GetValue(RegularBackgroundProperty); }
            set { SetValue(RegularBackgroundProperty, value); }
        }

        public static readonly DependencyProperty RegularBorderProperty =
            DependencyProperty.Register("RegularBorder", typeof (Brush), typeof (SwedenGridColorModel),
                                        new PropertyMetadata(Brushes.Black));

        public Brush RegularBorder
        {
            get { return (Brush) GetValue(RegularBorderProperty); }
            set { SetValue(RegularBorderProperty, value); }
        }

        public static readonly DependencyProperty QuestionTextProperty =
            DependencyProperty.Register("QuestionText", typeof (Brush), typeof (SwedenGridColorModel),
                                        new PropertyMetadata(Brushes.Black));

        public Brush QuestionText
        {
            get { return (Brush) GetValue(QuestionTextProperty); }
            set { SetValue(QuestionTextProperty, value); }
        }

        public static readonly DependencyProperty QuestionBackgroundProperty =
            DependencyProperty.Register("QuestionBackground", typeof (Brush), typeof (SwedenGridColorModel),
                                        new PropertyMetadata(Brushes.Yellow));

        public Brush QuestionBackground
        {
            get { return (Brush) GetValue(QuestionBackgroundProperty); }
            set { SetValue(QuestionBackgroundProperty, value); }
        }

        public static readonly DependencyProperty QuestionBorderProperty =
            DependencyProperty.Register("QuestionBorder", typeof (Brush), typeof (SwedenGridColorModel),
                                        new PropertyMetadata(Brushes.Black));

        public Brush QuestionBorder
        {
            get { return (Brush) GetValue(QuestionBorderProperty); }
            set { SetValue(QuestionBorderProperty, value); }
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            OnPropertyChanged(e.Property.Name);
        }

        private void OnPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    [PuzzleModel]
    public sealed class SwedenGridModel : GridPuzzleModel<SwedenGridCellInfo>
    {
        public const int GRID_SIZE = 64;

        public static readonly DependencyProperty ColorModelProperty =
            DependencyProperty.Register("ColorModel", typeof (SwedenGridColorModel), typeof (SwedenGridModel),
                                        new PropertyMetadata(default(SwedenGridColorModel)));

        public SwedenGridColorModel ColorModel
        {
            get { return (SwedenGridColorModel) GetValue(ColorModelProperty); }
            set
            {
                if (ColorModel != null)
                {
                    ColorModel.PropertyChanged -= ColorModel_PropertyChanged;
                }

                SetValue(ColorModelProperty, value);

                if (ColorModel != null)
                {
                    ColorModel.PropertyChanged += ColorModel_PropertyChanged;
                }
            }
        }

        public static readonly DependencyProperty SelectionGoDirectionProperty =
            DependencyProperty.Register("SelectionGoDirection", typeof (SelectionGoDirection), typeof (SwedenGridModel),
                                        new PropertyMetadata(SelectionGoDirection.None));

        public SelectionGoDirection SelectionGoDirection
        {
            get { return (SelectionGoDirection) GetValue(SelectionGoDirectionProperty); }
            set { SetValue(SelectionGoDirectionProperty, value); }
        }

        public SwedenGridModel()
            : base(GRID_SIZE, GRID_SIZE)
        {
            ColorModel = new SwedenGridColorModel();
        }

        private void ColorModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnPropertyChanged(e.PropertyName);
        }
    }
}
